home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Language/OS - Multiplatform Resource Library
/
LANGUAGE OS.iso
/
clean
/
sun3.lha
/
Sun3
/
seqdemos
/
hamming.icl
< prev
next >
Wrap
Text File
|
1992-08-07
|
2KB
|
64 lines
MODULE hamming;
<<
The Hamming Function.
The result of this program is a list of the first NrElements numbers
having only 2, 3 and 5 as prime factors. Result:
[1,2,3,4,5,6,8,9,10,12,15,16,18,20,24,25,...].
>>
IMPORT deltaI;
MACRO
NrElements -> 300; == The number of Hamming numbers to be calculated.
RULE
<< Ham (the Hamming function) returns an infinite list of numbers
having two, three and five as only prime factors by recursively
mapping the functions (*I 2), (*I 3) and (*I 5) over the list of
Hamming numbers found sofar and merging these lists into one.
>>
:: Ham -> [INT];
Ham -> y: [1 | Merge (Merge (Map (* 2) y) (Map (* 3) y)) (Map (* 5) y)];
<< Merge merges two sorted lists of integers into one. Elements that occur
more than once will be removed (every element should occur only once in
the final list of Hamming numbers).
>>
:: Merge [INT] [INT] -> [INT];
Merge [] [] -> [];
Merge f [] -> f;
Merge [] f -> f;
Merge f:[a|b] g:[c|d] -> [a | Merge b g], IF < a c
-> Merge f d , IF = a c
-> [c | Merge f d];
== The well-known Map function:
:: Map (=> x y) [x] -> [y];
Map f [] -> [];
Map f [a|b] -> [f a | Map f b];
== Take takes the first n elements of a (possibly infinite) list.
:: Take INT [x] -> [x];
Take 0 list -> [];
Take n [h|t] -> [h | Take (-- n) t];
Take n [] -> [];
<< The Start rule: take the first NrElements numbers from the
infinite list returned by the Hamming function (Ham).
>>
:: Start -> [INT];
Start -> Take NrElements Ham;